package by.kniazhych.core.view.frames {
	import by.kniazhych.core.events.ApplicationEvent;
	import by.kniazhych.core.frames.ApplicationFrame;
	import by.vaukalak.core.display.utils.AssetsAcsessor;
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import design.frames.RulesFrame;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
	
	
	public class RulesFrame extends BaseFrameView{
		private var _acsessor:AssetsAcsessor;
		
		private var _text:Object = { };
		private var _figure:Object = { };
		
		public function RulesFrame() {
			super();
			
			_text["kniaz"] = "гэта князь";
			_text["kniazhych"] = "гэта княжыч";
			_text["getman"] = "гэта гетман";
			_text["ratnik"] = "гэта ратнік";
			_text["laddzia"] = "гэта ладдзья";
			_text["garmata"] = "гэта гармата";
			_text["vaukalak"] = "гэта ваўкалак";
			_text["tron"] = "гэта трон";
			
			_acsessor = new AssetsAcsessor(new design.frames.RulesFrame());
			super.addChild(_acsessor["ramka"]);
			super.addChild(_exitButton);
			super.addChild(_figures);
			super.addChild(_descriptionText);
			super.addChild(_leftArrow);
			super.addChild(_rightArrow);
			_leftArrow.addEventListener(MouseEvent.CLICK, _onLeftArrowClicked);
			_rightArrow.addEventListener(MouseEvent.CLICK, _onRightArrowClicked);
			
			super.addChild(_figuresMask);
			_figures.mask = _figuresMask;
			_exitButton.addEventListener(MouseEvent.CLICK, _onExitButtonClick);
			
			for (var name:String in _text) {
				_addFigure(name);
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  PRIVATE METHODS
		//
		//--------------------------------------------------------------------------
		
		private function _addFigure(key:String):void {
			_figure[_figures.getChildByName(key)] = _text[key];
			Sprite(_figures.getChildByName(key)).buttonMode = true;
			InteractiveObject(_figures.getChildByName(key)).addEventListener(MouseEvent.CLICK, _onFigureClicked);
		}
		
		
		private var _currentFiguresIndex:int = 0;
		private var _positions:Array = new Array(0, 140, 280);
		private function _moveFiguresTable(direction:int):void {
			_currentFiguresIndex += direction;
			
			if (_currentFiguresIndex == -1) _currentFiguresIndex = _positions.length - 1;
			if (_currentFiguresIndex == _positions.length) _currentFiguresIndex = 0;
			
			
			//trace(_currentFiguresIndex);
			
			TweenLite.to(_figures, 0.7, { x: -_positions[_currentFiguresIndex] + 115} );
		}
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLERS
		//
		//--------------------------------------------------------------------------
		
		private function _onFigureClicked(e:MouseEvent):void {
			_descriptionText.text = _figure[e.currentTarget];
		}
		
		private function _onRightArrowClicked(e:MouseEvent):void {
			_moveFiguresTable(1);
		}
		
		private function _onLeftArrowClicked(e:MouseEvent):void {
			_moveFiguresTable( -1);
		}
		
		private function _onExitButtonClick(e:MouseEvent):void {
			dispatchEvent(new ApplicationEvent(ApplicationEvent.FRAME_CHANGED, ApplicationFrame.MENU));
		}
		
		//--------------------------------------------------------------------------
		//
		//  ASSETS ACSESSORS
		//
		//--------------------------------------------------------------------------
		
		private function get _exitButton():InteractiveObject {
			return _acsessor["exitButton"] as InteractiveObject;
		}
		private function get _figures():DisplayObjectContainer {
			return _acsessor["figures"] as DisplayObjectContainer;
		}
		
		private function get _leftArrow():InteractiveObject {
			return _acsessor["leftArrow"] as InteractiveObject;
		}
		
		private function get _rightArrow():InteractiveObject {
			return _acsessor["rightArrow"] as InteractiveObject;
		}
		
		private function get _figuresMask():DisplayObject {
			return _acsessor["figuresMask"] as DisplayObject;
		}
		
		private function get _descriptionText():TextField {
			return _acsessor["descriptionText"] as TextField;
		}
		
		
	}

}